from PyQt6.QtCore import (
    QFile, 
    QTextStream, 
    QIODevice, 
    QDataStream, 
    QDir, 
    QFileInfo,
    QStringConverter
)

def write_text_file():
    file = QFile("example.txt")
    if file.open(QIODevice.OpenModeFlag.WriteOnly | QIODevice.OpenModeFlag.Text):
        stream = QTextStream(file)
        stream << "第一行文本\n"
        stream << "第二行文本\n"
        stream << "第三行文本\n"
        file.close()
        print("文本文件写入成功")
    else:
        print("无法打开文件:", file.errorString())

def read_text_file():
    file = QFile("example.txt")
    if file.open(QIODevice.OpenModeFlag.ReadOnly | QIODevice.OpenModeFlag.Text):
        stream = QTextStream(file)
        stream.setEncoding(QStringConverter.Encoding.Utf8)
        
        print("\n文件全部内容:")
        print(stream.readAll())
        
        file.seek(0)
        print("\n逐行读取:")
        while not stream.atEnd():
            print(stream.readLine())
            
        file.close()
    else:
        print("无法打开文件:", file.errorString())

def write_binary_file():
    file = QFile("data.bin")
    if file.open(QIODevice.OpenModeFlag.WriteOnly):
        stream = QDataStream(file)
        stream.writeInt(42)
        stream.writeDouble(3.14159)
        stream.writeQString("PyQt6二进制数据")
        file.close()
        print("\n二进制文件写入成功")
    else:
        print("无法打开文件:", file.errorString())

def read_binary_file():
    file = QFile("data.bin")
    if file.open(QIODevice.OpenModeFlag.ReadOnly):
        stream = QDataStream(file)
        integer = stream.readInt()
        double_num = stream.readDouble()
        text = stream.readQString()
        
        print("\n从二进制文件读取:")
        print(f"整数: {integer}")
        print(f"浮点数: {double_num}")
        print(f"字符串: {text}")
        
        file.close()
    else:
        print("无法打开文件:", file.errorString())

def directory_operations():
    current_dir = QDir.current()
    print("\n当前目录:", current_dir.path())
    
    print("\n目录内容:")
    for entry in current_dir.entryList():
        print(entry)
    
    new_dir = QDir(current_dir.path() + "/new_directory")
    if not new_dir.exists():
        if current_dir.mkdir("new_directory"):
            print("\n目录创建成功")
    
    print("\n目录是否存在:", new_dir.exists())
    
    if new_dir.exists():
        if current_dir.rmdir("new_directory"):
            print("目录删除成功")

def file_operations():
    file_info = QFileInfo("example.txt")
    print("\n文件信息:")
    print("文件名:", file_info.fileName())
    print("文件大小:", file_info.size(), "字节")
    
    if file_info.exists():
        dir = QDir()
        if dir.rename("example.txt", "renamed_example.txt"):
            print("\n文件重命名成功")
    
    file_to_remove = QFile("renamed_example.txt")
    if file_to_remove.exists():
        if file_to_remove.remove():
            print("文件删除成功")

if __name__ == "__main__":
    print("=== PyQt6 文件操作示例 ===")
    
    # 文本文件操作
    write_text_file()
    read_text_file()
    
    # 二进制文件操作
    write_binary_file()
    read_binary_file()
    
    # 目录和文件操作
    directory_operations()
    file_operations()
    
    print("\n=== 示例结束 ===")